package com.feeyo.groundservice.recordnfc.activity.view

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.basicMarquee
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import com.blankj.utilcode.util.ActivityUtils
import com.feeyo.groundservice.base_util.getRouterPath
import com.feeyo.groundservice.composewidget.util.dimenToDp
import com.feeyo.groundservice.composewidget.util.dimenToSp
import com.feeyo.groundservice.composewidget.util.drawableToPainter
import com.feeyo.groundservice.composewidget.util.intervalClick
import com.feeyo.groundservice.composewidget.util.toComposeColor
import com.feeyo.groundservice.composewidget.view.edit.RoundedCornerTextFieldValue
import com.feeyo.groundservice.composewidget.view.util.HorizontalSpace
import com.feeyo.groundservice.composewidget.view.util.VerticalSpace
import com.feeyo.groundservice.recordnfc.R
import com.feeyo.groundservice.recordnfc.constant.AttachResourceType
import com.feeyo.groundservice.recordnfc.constant.RecordCardPath
import com.feeyo.groundservice.recordnfc.constant.ResourceType
import com.feeyo.groundservice.recordnfc.constant.currentSite
import com.feeyo.groundservice.recordnfc.constant.statusBarColor
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.interfaces.SimpleCallback

/**
 *作者：70893 on 2025-10-11
 *描述：
 */

@Composable
internal fun RowScope.RightMenuView(iatas: List<String>) {
    val currentIata = remember { mutableStateOf(iatas.first()) }

    Text(
        text = "卡管理",
        fontSize = R.dimen.d12.dimenToSp(),
        fontWeight = FontWeight.W500,
        color = R.color.white.toComposeColor(),
        modifier = Modifier.intervalClick {
            RecordCardPath.NfcLogActivity.getRouterPath().navigation()
        }
    )


    if (iatas.isNotEmpty()) {

        HorizontalSpace(R.dimen.d10.dimenToDp())
        Text(
            text = currentIata.value,
            fontSize = R.dimen.d12.dimenToSp(),
            fontWeight = FontWeight.W500,
            color = R.color.white.toComposeColor(),
            modifier = Modifier.intervalClick {
                if (iatas.size > 1) {
                    ActivityUtils.getTopActivity()?.let {
                        XPopup.Builder(it)
                            .dismissOnBackPressed(true)
                            .hasBlurBg(false)
                            .hasShadowBg(true)
                            .dismissOnTouchOutside(true)
                            .asCenterList(
                                "选择站点",
                                iatas.toTypedArray()
                            ) { position, text ->
                                currentIata.value = text
                                currentSite = text
                            }.show()
                    }
                }
            }
        )
    }


}

//选择的view
@Composable
internal fun SingleSelectView(
    title: String,
    content: String,
    hint: String,
) {
    Column(
        modifier = Modifier.fillMaxWidth().padding(R.dimen.d10.dimenToDp())
    ) {
        //标题
        Text(
            text = title,
            fontSize = R.dimen.d14.dimenToSp(),
            fontWeight = FontWeight.W500,
            color = R.color.black_33.toComposeColor()
        )
        VerticalSpace(R.dimen.d10.dimenToDp())
        // 内容
        Text(
            text = content.ifEmpty { hint },
            fontSize = R.dimen.d13.dimenToSp(),
            fontWeight = FontWeight.W400,
            color = if (content.isEmpty()) R.color.black_66.toComposeColor() else statusBarColor.toComposeColor(),
        )
    }
}

//位置选择
@Composable
internal fun PositionSelectView(
    content: String,
    onSelectType: (AttachResourceType) -> Unit,
    onDel: () -> Unit
) {
    val positionTypes = listOf(AttachResourceType.PARKING, AttachResourceType.GATE)
    //默认位置是机位类型
    val positionType = remember { mutableStateOf(positionTypes.first()) }

    val expand = remember { mutableStateOf(false) }
    val rotate = animateFloatAsState(
        targetValue = if (expand.value) 90f else 0f,
        label = "rotate"
    )

    //初始化一下
    LaunchedEffect(Unit) {
        onSelectType(positionType.value)
    }

    Column(
        modifier = Modifier.fillMaxWidth().padding(R.dimen.d10.dimenToDp())
    ) {
        Row(
            modifier = Modifier.intervalClick {
                ActivityUtils.getTopActivity()?.let {
                    XPopup.Builder(it)
                        .dismissOnBackPressed(true)
                        .hasBlurBg(false)
                        .hasShadowBg(true)
                        .dismissOnTouchOutside(true)
                        .setPopupCallback(object : SimpleCallback() {
                            override fun beforeShow(popupView: BasePopupView?) {
                                super.beforeShow(popupView)
                                expand.value = !expand.value
                            }

                            override fun onDismiss(popupView: BasePopupView?) {
                                super.onDismiss(popupView)
                                expand.value = !expand.value
                            }
                        })
                        .asBottomList(
                            "请选择位置类型",
                            positionTypes.map { it.title }.toTypedArray(),
                        ) { position, text ->
                            positionType.value = positionTypes[position]
                            onSelectType(positionType.value)

                        }.show()
                }
            },
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = positionType.value.title,
                fontSize = R.dimen.d14.dimenToSp(),
                fontWeight = FontWeight.W500,
                color = R.color.black_33.toComposeColor()
            )
            HorizontalSpace(R.dimen.d5.dimenToDp())
            Icon(
                painter = R.drawable.icon_arrow_right.drawableToPainter(),
                contentDescription = null,
                modifier = Modifier.size(R.dimen.d15.dimenToDp())
                    .rotate(rotate.value),
                tint = R.color.black_66.toComposeColor()
            )
        }

        VerticalSpace(R.dimen.d10.dimenToDp())

        // 内容
        if (content.isNotEmpty())
            Box(
                modifier = Modifier.height(R.dimen.d30.dimenToDp())
            ) {
                Text(
                    text = content,
                    fontSize = R.dimen.d13.dimenToSp(),
                    fontWeight = FontWeight.W400,
                    color = R.color.white.toComposeColor(),
                    textAlign = TextAlign.Center,
                    maxLines = 1,
                    modifier = Modifier
                        .widthIn(min = R.dimen.d50.dimenToDp())
                        .align(Alignment.BottomStart)
                        .padding(top = R.dimen.d8.dimenToDp(), end = R.dimen.d8.dimenToDp())
                        .background(
                            color = statusBarColor.toComposeColor(),
                            shape = RoundedCornerShape(R.dimen.d3.dimenToDp())
                        ).padding(R.dimen.d2.dimenToDp())
                        .basicMarquee(iterations = Int.MAX_VALUE)
                )

                Image(
                    painter = R.drawable.icon_edt_clear_gray.drawableToPainter(),
                    contentDescription = null,
                    modifier = Modifier.size(R.dimen.d16.dimenToDp())
                        .align(Alignment.TopEnd)
                        .intervalClick {
                            //删除
                            onDel()
                        }
                )
            }
        else
            Text(
                text = positionType.value.hint,
                fontSize = R.dimen.d13.dimenToSp(),
                fontWeight = FontWeight.W400,
                color = R.color.black_66.toComposeColor(),
            )
    }


}

//选择的view
@Composable
internal fun MultiSelectView(
    title: String,
    content: List<String>,
    hint: String,
    onDel: (Int) -> Unit
) {
    val column = 3//列数
    val itemHeight = R.dimen.d30.dimenToDp()
    val itemSpace = R.dimen.d5.dimenToDp()
    val maxLine = 2
    val totalHeight =
        (if (content.size % column == 0) content.size / column else content.size / column + 1).let {
            val lastLines = if (it >= maxLine) maxLine else it
            //根据行数计算总高度
            itemHeight * lastLines + itemSpace * (lastLines - 1)
        }
    Column(
        modifier = Modifier.fillMaxWidth().padding(R.dimen.d10.dimenToDp())
    ) {
        //标题
        Text(
            text = title,
            fontSize = R.dimen.d14.dimenToSp(),
            fontWeight = FontWeight.W500,
            color = R.color.black_33.toComposeColor()
        )
        VerticalSpace(R.dimen.d10.dimenToDp())
        // 内容
        if (content.isEmpty()) {
            Text(
                text = hint,
                fontSize = R.dimen.d14.dimenToSp(),
                fontWeight = FontWeight.W400,
                color = R.color.black_66.toComposeColor(),
            )
        } else {
            LazyVerticalGrid(
                columns = GridCells.Fixed(count = column),
                modifier = Modifier.fillMaxWidth().height(totalHeight),
                horizontalArrangement = Arrangement.spacedBy(itemSpace),
                verticalArrangement = Arrangement.spacedBy(itemSpace)
            ) {
                itemsIndexed(content) { index, it ->
                    Box(
                        modifier = Modifier.fillMaxWidth().height(itemHeight)
                    ) {
                        Text(
                            text = it,
                            fontSize = R.dimen.d13.dimenToSp(),
                            fontWeight = FontWeight.W400,
                            color = R.color.white.toComposeColor(),
                            maxLines = 1,
                            modifier = Modifier.align(Alignment.BottomStart)
                                .padding(top = R.dimen.d8.dimenToDp(), end = R.dimen.d8.dimenToDp())
                                .background(
                                    color = statusBarColor.toComposeColor(),
                                    shape = RoundedCornerShape(R.dimen.d3.dimenToDp())
                                ).padding(R.dimen.d2.dimenToDp())
                                .basicMarquee(iterations = Int.MAX_VALUE)
                        )

                        Image(
                            painter = R.drawable.icon_edt_clear_gray.drawableToPainter(),
                            contentDescription = null,
                            modifier = Modifier.size(R.dimen.d16.dimenToDp())
                                .align(Alignment.TopEnd)
                                .intervalClick {
                                    //删除
                                    onDel(index)
                                }
                        )
                    }
                }
            }
        }

    }
}

//带输入框的view
@Composable
internal fun EditView(
    title: String,
    content: String,
    hint: String,
    onChange: (String) -> Unit
) {
    Column(
        modifier = Modifier.fillMaxWidth().padding(R.dimen.d10.dimenToDp())
    ) {
        //标题
        Text(
            text = title,
            fontSize = R.dimen.d14.dimenToSp(),
            fontWeight = FontWeight.W500,
            color = R.color.black_33.toComposeColor()
        )
        VerticalSpace(R.dimen.d5.dimenToDp())
        // 内容
        RoundedCornerTextFieldValue(
            modifier = Modifier.height(R.dimen.d30.dimenToDp()),
            onValueChange = {
                onChange(it)
            },
            content = content,
            hint = hint,
            hintColor = R.color.black_66.toComposeColor(),
            hintSize = R.dimen.d14.dimenToSp(),
            textStyle = TextStyle(
                fontSize = R.dimen.d14.dimenToSp(),
                fontWeight = FontWeight.W400
            ),
            hasLeadingIcon = false,
            focusedTextColor = statusBarColor.toComposeColor(),
            unfocusedTextColor = R.color.black.toComposeColor(),
            focusedContainerColor = R.color.white.toComposeColor(),
            unfocusedContainerColor = R.color.white.toComposeColor(),
            cursorColor = R.color.blue_main_2E76EE.toComposeColor(),
        )
    }
}